package yl.hs.bmipfull.utils;

import lombok.experimental.UtilityClass;
import yl.hs.bmipfull.viewmodels.salemanmage.SetTimeVO;
import yl.hs.bmipfull.viewmodels.salemanmage.TimeGroupVO;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张凯
 * @apiNote
 * @since 2022/3/4 10:40 上午 创建
 */
@UtilityClass
public class VoUtil {
    /**
     * 聚合时间
     * @param setTime 时间信息
     * @return 聚合后说的时间信息
     */
    public  List<TimeGroupVO> groupTime(List<SetTimeVO> setTime) {
        List<TimeGroupVO> timeGroupList = new ArrayList<>();
        List<String> years = setTime.stream().map(SetTimeVO::getYear).distinct().collect(Collectors.toList());
        years.forEach(t ->{
            if (t != null) {
                TimeGroupVO setTimeGroupVO = new TimeGroupVO();
                setTimeGroupVO.setYear(t);
                List<String> monthList = setTime.stream().filter(f -> t.equals(f.getYear())).map(SetTimeVO::getMonth).collect(Collectors.toList());
                setTimeGroupVO.setMonth(monthList);
                timeGroupList.add(setTimeGroupVO);
            }
        });
        if (timeGroupList.size() < 1){
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            TimeGroupVO setTimeGroupVO = new TimeGroupVO();
            setTimeGroupVO.setYear(String.valueOf(year));
            List<String> monthList = new ArrayList<>();
            monthList.add(String.valueOf(month));
            setTimeGroupVO.setMonth(monthList);
            timeGroupList.add(setTimeGroupVO);
        }
        return timeGroupList;
    }

}
